package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 要什么？给什么？
     * 多个返回值：封装（只有一个）
     *
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result<PageBean<Emp>> selectPage(
            String name,
            Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageBean<Emp> empPageBean = empService.selectPage(
                name,
                gender,
                begin,
                end,
                page,
                pageSize);

        return Result.success(empPageBean);


    }

    /**
     * query参数的批量传参：?hobby=唱&hobby=跳
     * path参数的批量传参：/emps/1,2,3,4,5,6
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result<?> deleteById(@PathVariable List<Integer> ids) {
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 新增员工信息
     *
     * @param emp
     * @return
     */
    @PostMapping
    public Result<?> create(@RequestBody Emp emp) {
        empService.create(emp);
        return Result.success();
    }

    /**
     * 根据ID查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Emp> queryById(@PathVariable Integer id) {
        Emp emp = empService.queryById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result<?> updateById(@RequestBody Emp emp) {
        empService.updateById(emp);
        return Result.success();
    }
}
